home *** CD-ROM | disk | FTP | other *** search
- #include "graphics.h"
- #include "about.h"
- #include "about MSG.h"
- #include "help.h"
- //#include "progress.h"
- #include "dialogs.h"
- #include "error.h"
- #include "menus.h"
- #include "environment.h"
- #include "prefs.h"
- #include "util.h"
- #include "program globals.h"
-
- /* internal global variables for use by graphics.c only */
- static ExtendedWindowDataHandle gTheWindowData[NUM_WINDOWS];
- static Rect gBoundsRect[NUM_WINDOWS]; /* rectangle of offscreen bitmap */
- static Rect gMainScreenBounds; /* bounds of main monitor */
- static GWorldPtr gTheGWorld[NUM_WINDOWS]; /* offscreen graphics world */
- static Ptr gBWBitMap[NUM_WINDOWS]; /* offscreen bitmap for B/W machines */
- static GrafPort gBWGrafPort[NUM_WINDOWS]; /* offscreen grafport " " " */
- static GrafPtr gBWGrafPtr[NUM_WINDOWS]; /* offscreen grafptr " " " */
-
- Boolean InitTheGraphics(void)
- {
- short i,j;
-
- GetMainScreenBounds();
-
- for (i=0; i<NUM_WINDOWS; i++)
- {
- /* nothing is inited; if there's an error later on, we'll know how much to */
- /* clean up in ShutDownTheGraphics() */
- gTheGWorld[i]=(GWorldPtr)0L;
- gTheWindowData[i]=(ExtendedWindowDataHandle)0L;
- }
-
- for (i=0; i<NUM_WINDOWS; i++)
- {
- gTheWindowData[i]=(ExtendedWindowDataHandle)NewHandle(sizeof(ExtendedWindowDataRec));
- if (gTheWindowData[i]==0L) /* return if error */
- return FALSE;
-
- (**(gTheWindowData[i])).offscreenNeedsUpdate=TRUE; /* offscreen not inited */
- (**(gTheWindowData[i])).theWindowPtr=0L; /* window ptr not inited */
- (**(gTheWindowData[i])).windowIndex=i; /* so we can retrieve it O(1) */
- (**(gTheWindowData[i])).windowDepth=
- (**(gTheWindowData[i])).maxDepth=1; /* init at B/W */
- (**(gTheWindowData[i])).isColor=FALSE; /* init to grayscale */
- (**(gTheWindowData[i])).topLeftInitted=FALSE; /* window pos not initted */
- for (j=0; j<MAX_TE_HANDLES; j++)
- (**(gTheWindowData[i])).hTE[j]=0L;
- HLockHi((Handle)gTheWindowData[i]);
- }
-
- /* set window dispatch routines */
- SetIndDispatchProc(kAboutWindow, AboutBoxDispatch);
- SetIndDispatchProc(kAboutMSGWindow, AboutMSGBoxDispatch);
- // SetIndDispatchProc(kProgressWindow, ProgressWindowDispatch);
- SetIndDispatchProc(kHelpWindow, HelpWindowDispatch);
-
- /* call window dispatch routines with "startup" message */
- CallIndDispatchProc(kAboutWindow, kStartup, 0L);
- CallIndDispatchProc(kAboutMSGWindow, kStartup, 0L);
- // CallIndDispatchProc(kProgressWindow, kStartup, 0L);
- CallIndDispatchProc(kHelpWindow, kStartup, 0L);
-
- return TRUE;
- }
-
- void ShutDownTheGraphics(void)
- {
- short i;
- ExtendedWindowDataHandle theData;
-
- /* send shutdown messages to the shell's windows */
- CallIndDispatchProc(kAboutWindow, kShutdown, 0L);
- CallIndDispatchProc(kAboutMSGWindow, kShutdown, 0L);
- // CallIndDispatchProc(kProgressWindow, kShutdown, 0L);
- CallIndDispatchProc(kHelpWindow, kShutdown, 0L);
-
- for (i=0; i<NUM_WINDOWS; i++)
- {
- KillOffscreen(i);
- if ((theData=GetWindowDataHandle(i))!=0L)
- {
- if (IndWindowExistsQQ(i))
- DisposeWindow(GetIndWindowPtr(i));
- DisposeHandle((Handle)theData);
- }
- }
- }
-
- OSErr OpenTheIndWindow(short index)
- {
- WindowPtr theWindow;
- Point topLeft;
- Boolean justCenteredIt;
- OSErr memError;
-
- if (!IndWindowExistsQQ(index))
- {
- justCenteredIt=FALSE;
-
- if (CallIndDispatchProc(index, kInitialize, 0L)==kFailure)
- { /* default is to center window on main screen */
- if (!GetIndWindowPosInitted(index))
- {
- topLeft=GetIndWindowPos(index);
- topLeft.h=gMainScreenBounds.left + (((gMainScreenBounds.right -
- gMainScreenBounds.left) - (**(gTheWindowData[index])).windowWidth) / 2);
- topLeft.v=gMainScreenBounds.top + (((gMainScreenBounds.bottom -
- gMainScreenBounds.top) - (**(gTheWindowData[index])).windowHeight) / 2);
- SetIndWindowPos(index, topLeft);
- justCenteredIt=TRUE;
- }
- }
-
- topLeft=GetIndWindowPos(index);
- (**(gTheWindowData[index])).windowBounds.left=topLeft.h;
- (**(gTheWindowData[index])).windowBounds.top=topLeft.v;
-
- if (justCenteredIt)
- if (((**(gTheWindowData[index])).windowType==noGrowDocProc) ||
- ((**(gTheWindowData[index])).windowType==documentProc) ||
- ((**(gTheWindowData[index])).windowType==movableDBoxProc) ||
- ((**(gTheWindowData[index])).windowType==zoomDocProc) ||
- ((**(gTheWindowData[index])).windowType==zoomNoGrow) ||
- ((**(gTheWindowData[index])).windowType==rDocProc))
- (**(gTheWindowData[index])).windowBounds.top += 9; /* compensate for title */
-
- /* don't put window over menu bar */
- if ((**(gTheWindowData[index])).windowBounds.top < GetMBarHeight()+1)
- (**(gTheWindowData[index])).windowBounds.top = GetMBarHeight()+1;
-
- (**(gTheWindowData[index])).windowBounds.bottom =
- (**(gTheWindowData[index])).windowBounds.top +
- (**(gTheWindowData[index])).windowHeight;
-
- (**(gTheWindowData[index])).windowBounds.right =
- (**(gTheWindowData[index])).windowBounds.left +
- (**(gTheWindowData[index])).windowWidth;
-
- KillOffscreen(index); /* kill offscreen bitmaps that are left over */
-
- if (gHasColorQD)
- {
- /* create the color window with our specs, see IM Essentials 4-79ff */
- (**gTheWindowData[index]).theWindowPtr=
- NewCWindow(0L, &((**(gTheWindowData[index])).windowBounds),
- (**(gTheWindowData[index])).windowTitle, FALSE,
- (**(gTheWindowData[index])).windowType, (WindowPtr)-1L,
- (**(gTheWindowData[index])).hasCloseBox,
- (unsigned long)gTheWindowData[index]);
- }
- else
- {
- /* create the B/W window with our specs, see IM Essentials 4-82ff */
- (**gTheWindowData[index]).theWindowPtr=
- NewWindow(0L, &((**(gTheWindowData[index])).windowBounds),
- (**(gTheWindowData[index])).windowTitle, FALSE,
- (**(gTheWindowData[index])).windowType, (WindowPtr)-1L,
- (**(gTheWindowData[index])).hasCloseBox,
- (unsigned long)gTheWindowData[index]);
- }
-
- memError=MemError();
- }
-
- if (IndWindowExistsQQ(index))
- {
- theWindow=GetIndWindowPtr(index);
- ShowWindow(theWindow); /* immediately show this new window */
- SelectWindow(theWindow); /* immediately select this new window */
- SetPort(theWindow); /* important! for TE info to stick*/
- InvalRect(&(theWindow->portRect));
- /* call window's dispatch routine to alert it that it's open now */
- CallIndDispatchProc(index, kOpen, 0L);
- }
- else return memError;
-
- return noErr;
- }
-
- void GetMainScreenBounds(void)
- {
- gMainScreenBounds = qd.screenBits.bounds; /* low-mem global */
- gMainScreenBounds.top += GetMBarHeight(); /* don't include menu bar */
- }
-
- short GetBiggestDeviceDepth(ExtendedWindowDataHandle theData)
- {
- Rect tempRect;
- long biggestSize;
- long tempSize;
- GDHandle thisHandle, gBiggestDevice;
-
- if (!gHasColorQD)
- return 1;
-
- if (!WindowExistsQQ(theData))
- return (**(**GetMainDevice()).gdPMap).pixelSize;
-
- thisHandle = GetDeviceList();
- gBiggestDevice = 0L;
- biggestSize = 0L;
-
- while (thisHandle)
- {
- if (TestDeviceAttribute(thisHandle, screenDevice) &&
- TestDeviceAttribute(thisHandle, screenActive))
- {
- if (SectRect(&(GetWindowPtr(theData)->portRect),
- &((**thisHandle).gdRect), &tempRect))
- {
- if (biggestSize < (tempSize = ((long)(tempRect.bottom - tempRect.top))*
- ((long)(tempRect.right - tempRect.left))))
- {
- biggestSize = tempSize;
- gBiggestDevice = thisHandle;
- }
- }
- }
- thisHandle = GetNextDevice(thisHandle);
- }
-
- return (gBiggestDevice) ? (**(**gBiggestDevice).gdPMap).pixelSize : 1;
- }
-
- short GetIndWindowDepth(short index)
- {
- /* if Color Quickdraw is not available, the depth must be 1. */
- /* if Color Quickdraw is available and the window exists, return the window's
- GWorld's graphics device's pixel map's pixel depth */
- /* if Color Quickdraw is available and the window does not exist, return the
- pixel depth of the main screen */
- return (gHasColorQD) ?
- (IndWindowExistsQQ(index) ?
- (**(**(GetGWorldDevice((GWorldPtr)GetIndOffscreenPtr(index)))).gdPMap).pixelSize :
- (**(**GetMainDevice()).gdPMap).pixelSize) :
- 1;
- }
-
- Boolean WindowIsColor(ExtendedWindowDataHandle theData)
- {
- return (gHasColorQD) ?
- ((GetWindowDepth(theData)>8) ?
- TRUE :
- TestDeviceAttribute(GetGWorldDevice((GWorldPtr)GetOffscreenPtr(theData)),
- gdDevType)) :
- FALSE;
- }
-
- void UpdateTheWindow(ExtendedWindowDataHandle theData)
- {
- short index;
- long offRowBytes, sizeOfOff;
- unsigned long updateResult;
- Boolean isColor;
- PixMapHandle thePixMapHandle;
- GWorldPtr currentGWorld;
- GDHandle currentGDHandle;
-
- index=GetWindowIndex(theData);
- gBoundsRect[index]=GetIndWindowPtr(index)->portRect;
- OffsetRect(&gBoundsRect[index], -gBoundsRect[index].left, -gBoundsRect[index].top);
-
- if (gHasColorQD) /* w/o Color Quickdraw, GWorlds may not be supported */
- {
- if (gTheGWorld[index]==0L) /* create new graphics world if none exists */
- {
- /* try to create new graphics world; display error if unsuccessful */
- if (NewGWorld(&gTheGWorld[index],
- (GetBiggestDeviceDepth(theData)>=GetWindowData_maxDepth(theData)) ?
- (**theData).maxDepth : 0, &gBoundsRect[index], 0L, 0L, 0)!=0)
- {
- HandleError(kNoMemory, TRUE); /* quits */
- }
-
- SetWindowData_windowDepth(theData, GetWindowDepth(theData));
- NoPurgePixels(GetGWorldPixMap(gTheGWorld[index])); /* never purge our pixmap! */
- updateResult=1;
- }
- else updateResult=0;
-
- GetGWorld(¤tGWorld, ¤tGDHandle); /* get current settings */
- LockPixels(thePixMapHandle=GetGWorldPixMap(gTheGWorld[index])); /* important! copybits may move mem */
- /* update offscreen graphics world, compensating for change in pixel depth */
- updateResult|=(unsigned long)UpdateGWorld(&gTheGWorld[index],
- (GetBiggestDeviceDepth(theData)>=GetWindowData_maxDepth(theData)) ?
- GetWindowData_maxDepth(theData) : 0, &gBoundsRect[index], 0L, 0L, 0);
- SetGWorld(gTheGWorld[index], 0L); /* set to our offscreen gworld */
-
- isColor=WindowIsColor(theData);
- if (isColor!=GetWindowData_isColor(theData))
- {
- SetWindowData_isColor(theData, isColor);
- updateResult=1;
- }
-
- if ((updateResult!=0L) || (GetWindowData_windowDepth(theData)!=GetWindowDepth(theData)))
- {
- SetWindowData_windowDepth(theData, GetWindowDepth(theData));
- SetWindowData_offscreenNeedsUpdate(theData, TRUE);
- CallDispatchProc(theData, kChangeDepth, 0L);
- }
- }
- else /* deal with (guaranteed) B/W bitmaps manually */
- {
- if (gBWGrafPtr[index]==0L) /* create new offscreen bitmap if none exists */
- {
- gBWGrafPtr[index]=&gBWGrafPort[index];
- OpenPort(gBWGrafPtr[index]); /* make a new port */
-
- /* calculate the size of the offscreen bitmap from the boundsrect */
- offRowBytes=(((gBoundsRect[index].right-gBoundsRect[index].left)+15)>>4)<<1;
- sizeOfOff=(long)(gBoundsRect[index].bottom-gBoundsRect[index].top)*offRowBytes;
-
- gBWBitMap[index]=NewPtr(sizeOfOff); /* allocate space for bitmap */
- if (gBWBitMap[index]==0L) /* abort if unsuccessful */
- {
- ClosePort(gBWGrafPtr[index]); /* cleaning up... */
- gBWGrafPtr[index]=0L;
- HandleError(kNoMemory, TRUE); /* displaying error... */
- }
-
- gBWGrafPort[index].portBits.baseAddr=gBWBitMap[index]; /* --> our bitmap */
- gBWGrafPort[index].portBits.rowBytes=offRowBytes; /* bitmap size */
- gBWGrafPort[index].portBits.bounds= /* bitmap bounds */
- gBWGrafPort[index].portRect=gBoundsRect[index];
-
- SetPort(gBWGrafPtr[index]);
- SetWindowData_offscreenNeedsUpdate(theData, TRUE);
- }
- else SetPort(gBWGrafPtr[index]); /* set port for subsequent drawing */
- }
-
- if (GetWindowData_offscreenNeedsUpdate(theData))
- {
- SetWindowData_offscreenNeedsUpdate(theData, FALSE);
- /* call window's dispatch and tell it to redraw itself */
- CallDispatchProc(theData, kUpdate, GetWindowDepth((WindowDataHandle)theData));
- }
-
- if (gHasColorQD)
- SetGWorld(currentGWorld, currentGDHandle); /* restore old settings */
-
- SetPort(GetWindowPtr(theData));
-
- /* copy offscreen bitmap from graphics world or bitmap to onscreen window */
- if (CallDispatchProc(theData, kCopybits, (unsigned long)GetOffscreenPtr(theData))==kFailure)
- {
- CopyBits( &(GetOffscreenPtr(theData)->portBits),
- &(GetWindowPtr(theData)->portBits),
- &gBoundsRect[index], &gBoundsRect[index], 0, 0L);
- }
-
- for (index=0; index<MAX_TE_HANDLES; index++)
- {
- if (GetWindowData_hTE(theData, index)!=0L)
- {
- EraseRect(&(**GetWindowData_hTE(theData, index)).viewRect);
- TEUpdate(&(GetWindowPtr(theData)->portRect),
- GetWindowData_hTE(theData, index));
- }
- }
-
- ValidRect(&(GetWindowPtr(theData)->portRect)); /* so we don't reupdate */
-
- if (gHasColorQD)
- UnlockPixels(thePixMapHandle); /* remember we locked these? */
-
- KillOffscreen(GetWindowIndex(theData));
- }
-
- Boolean CloseTheWindow(ExtendedWindowDataHandle theData)
- {
- /* if the window's dispatch cancels the close, abort */
- if (CallDispatchProc(theData, kClose, 0L)==kCancel)
- return FALSE;
-
- /* get rid of the actual window in memory */
- DisposeWindow(GetWindowPtr(theData));
-
- /* set window pointer to 0L so we know it's gone */
- SetWindowData_windowPtr(theData, 0L);
-
- /* kill offscreen bitmaps left over */
- KillOffscreen(GetWindowIndex(theData));
-
- /* tell window's dispatch that it's disposed of now */
- CallDispatchProc(theData, kDispose, 0L);
-
- return TRUE; /* successful close */
- }
-
- PicHandle DrawThePicture(PicHandle thePict, short whichPict, short x, short y)
- /* a standard routine for loading a picture (if necessary) and then drawing it */
- {
- Rect temp;
-
- if (thePict==0L) /* get it if it doesn't exist */
- thePict=(PicHandle)GetPicture(whichPict);
-
- HLock((Handle)thePict); /* lock it down for dereferencing to get picture bounds */
- temp.top=y;
- temp.left=x;
- temp.bottom=temp.top+(**thePict).picFrame.bottom-(**thePict).picFrame.top;
- temp.right=temp.left+(**thePict).picFrame.right-(**thePict).picFrame.left;
- DrawPicture(thePict, &temp); /* draw picture at (x,y) */
- HUnlock((Handle)thePict); /* unlock for better memory management */
- return thePict;
- }
-
- PicHandle ReleaseThePict(PicHandle thePict)
- {
- if (thePict!=0L) /* if exists, release it */
- ReleaseResource((Handle)thePict);
- return 0L;
- }
-
- short CallDispatchProc(ExtendedWindowDataHandle theData, short theMessage, unsigned long misc)
- {
- return (GetWindowData_dispatchProc(theData))((WindowDataHandle)theData, theMessage, misc);
- }
-
- void KillOffscreen(short index)
- {
- if (gTheGWorld[index]!=0L)
- DisposeGWorld(gTheGWorld[index]);
- if (gBWGrafPtr[index]!=0L)
- DisposePtr((Ptr)gBWGrafPtr[index]);
- gTheGWorld[index]=0L;
- gBWGrafPtr[index]=0L;
- (**(gTheWindowData[index])).offscreenNeedsUpdate=TRUE;
- CompactMem(maxSize);
- }
-
- ExtendedWindowDataHandle GetWindowDataHandle(short index)
- {
- return gTheWindowData[index];
- }
-
- Point GetWindowBoundsTopLeft(WindowDataHandle theData)
- {
- Point tempPt;
- Rect tempRect;
-
- tempRect=GetWindowData_windowBounds(theData);
- tempPt.h=tempRect.left;
- tempPt.v=tempRect.top;
-
- return tempPt;
- }
-
- GrafPtr GetOffscreenPtrFunction(WindowDataHandle theData)
- {
- return (GrafPtr)(GetOffscreenPtr(theData));
- }
-